<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.account"
        placeholder="账号"
        style="width:200px"
        class="filter-item"
        size="mini"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-input
        v-model="listQuery.nickName"
        placeholder="用户名"
        style="width:200px"
        class="filter-item"
        size="mini"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="状态"
        style="width:200px"
        class="filter-item"
        size="mini"
        clearable
        @change="handleFilter"
      >
        <el-option label="未批改" :value="1">未批改</el-option>
        <el-option label="已批改" :value="0">已批改</el-option>
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        size="mini"
        style="margin-left:10px;"
        @click="handleFilter"
      >查询</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoadding"
      :data="list"
      size="mini"
      border
      fit
      highlight-current-row
      style="width:100%"
      @sort-change="sortChange"
    >
      <el-table-column
        label="序号"
        prop="id"
        sortable="custom"
        align="center"
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="账号"
        align="center"
        width="120"
      >
        <template slot-scope="{ row: { account }}">
          <span>{{ account }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="用户名"
        align="center"
        width="120"
      >
        <template slot-scope="{ row: { nickName }}">
          <span>{{ nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        width="80"
      >
        <template slot-scope="{ row: { status }}">
          <el-tag :type="status==1?'danger':'success'">{{ status==1?'未批改':'已批改' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作题作业"
        align="center"
        width="460"
      >
        <template slot-scope="{ row: { mediaUrl }}">
          <a style="color:#909399;text-decoration: underline;" :href="mediaUrl" target="_blank">可点击下载学生操作作业进行批改</a>
        </template>
      </el-table-column>
      <el-table-column
        label="选择题分数(总：60)"
        align="center"
        width="110"
      >
        <template slot-scope="{ row: { chooseScore }}">
          <span>
            {{ chooseScore }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作题分数（总：40）"
        align="center"
        width="110"
      >
        <template slot-scope="{row,$index}">
          <el-input v-if="showEdit[$index]" v-model="row.operationScore" class="edit-cell" />
          <span v-if="!showEdit[$index]">{{ row.operationScore }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="190"
      >
        <template slot-scope="{row,$index}">
          <el-button v-if="showBtn[$index]" type="success" size="small" @click.native="handleUpdate($index, row)">保存</el-button>
          <el-button v-if="showBtn[$index]" type="warning" size="small" @click.native="handelCancel($index, row)">取消</el-button>

          <el-button v-if="!showBtn[$index]" type="primary" size="small" @click.native="handleEdit($index, row)">编辑</el-button>
          <!-- <el-button v-if="!showBtn[$index]" type="danger" size="small" @click.native="handleDelete($index, row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>=0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="handlePageList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import { getAnswer, scoring } from '@/api/teachplan'
export default {
  inject: ['reload'],
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    const generateData = _ => {
      const data = []
      for (let i = 1; i <= 15; i++) {
        data.push({
          key: i,
          label: `学号 ${i}`,
          disabled: i % 4 === 0
        })
      }
      return data
    }
    return {
      tableKey: 0,
      listLoadding: true,
      list: [],
      total: 0,
      listQuery: {
      },
      dialogFormVisible: false,
      data: generateData(),
      value: [1],
      value4: [1],
      showEdit: [], // 显示编辑框
      showBtn: [],
      showBtnOrdinary: true,
      renderFunc(h, option) {
        return <span>{ option.key } - { option.label }</span>
      }
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getList()
  },
  methods: {
    parseQuery() {
      // 解析查询条件
      const liatQuery = {
        page: 1,
        pageSize: 10
      }
      // 合并查询条件
      this.listQuery = { ...liatQuery, ...this.listQuery }
    },
    getList() {
      this.listLoadding = true
      // 调用接口
      getAnswer(this.listQuery, this.$route.query.teachplanId).then(response => {
        console.log(response.data)
        this.list = response.data.content
        this.total = response.data.totalSize
      })
      this.listLoadding = false
    },
    handleFilter() {
      this.list = this.getList()
    },
    // 点击不同页时调用的方法
    handlePageList() {
      this.list = this.getList()
    },
    sortChange(data) {
      console.log('排序' + data)
    },
    handleChange(value, direction, movedKeys) {
      console.log(value, direction, movedKeys)
    },
    // 点击编辑
    handleEdit(index, row) {
      // this.showEdit[index] = true
      // this.showBtn[index] = true
      this.$set(this.showEdit, index, true)
      this.$set(this.showBtn, index, true)
    },
    // 取消编辑
    handelCancel(index, row) {
      this.$set(this.showEdit, index, false)
      this.$set(this.showBtn, index, false)
      // this.showEdit[index] = false
      // this.showBtn[index] = false
    },

    // 点击保存
    handleUpdate(index, row) {
      console.log(row)
      scoring(row.id, row.operationScore, row.userId).then(response => {
        this.$notify({
          title: '成功',
          message: response.msg,
          type: 'success',
          duration: 2000
        })
        this.$set(this.showEdit, index, false)
        this.$set(this.showBtn, index, false)
        this.getList()
      })
    }
  }
}
</script>

<style>
.transfer-footer {
    margin-left: 20px;
    padding: 6px 5px;
  }
</style>
